(function($) {
	$.fn.extend({

		jsonengine : {

			_url : 'http://localhost:8888/_je/',
			_docType : '',
			_filter  : [],
			_sort    : [],
			_limit   : '',
			_offset  : '',

			_data: '',

			_success: function(data, dataType){},
			_error: function(xhr, textStatus, errorThrown){},

			//private
			clear: function(){
				this._filter = [];
				this._sort = [];
				this._limit = '';
				this._offset = '';
				this._data = '';
				this._success = function(data, dataType){};
				this._error = function(xhr, textStatus, errorThrown){};
			},

			//private
			log : function(e1, e2, e3) {
				console.log(e1);
				console.log(e2);
				console.log(e3);
			},

			//private
			isString : function(e) {
				return Object.prototype.toString.call(e)=='[object String]';
			},

			//private
			isArray : function(e) {
				return Object.prototype.toString.call(e)=='[object Array]';
			},

			//private
			toObject : function(config, key){
				var tmp = {};
				if(key){
					tmp[key] = {};
					if(this.isString(config))
						tmp[key][0]=config;
					else
						if(this.isArray(config))
							for(var i in config)
								tmp[key][i] = config[i];
						else
							for(var i in config)
								tmp[key][i] = i+'.'+config[i];
				}
				else{
					if(this.isString(config))
						tmp[0]=config;
					else
						if(this.isArray(config))
							for(var i in config)
								tmp[i] = config[i];
						else
							for(var i in config)
								tmp[i] = i+'.'+config[i];
				}
				return tmp;
			},

			//private
			setArray : function(config, array, str) {
				var k = 'key';
				config = this.toObject(config, k);
				var e = config[k];
				for(var i in e)
					array.push(str+e[i]);
			},

			//private
			ajax: function(type){
				$.ajax({
//					async:false,
					type   : type,
					url    : this._url + this._docType,
					data   : this._data,
					success: this._success,
					error  : this._error
				});
				return 1;
			},

			//=== PUBLIC ===
			docType : function(config) {
				this._docType = config;
				return this;
			},

			//=== PUBLIC ===
			query : function(config) {
				if(!config) return this;
				this._docType = config;
				return this;
			},

			//=== PUBLIC ===
			filter : function(config) {
				var str = (this.isString(config))? '': 'cond=';
				this.setArray(config, this._filter, str);
				return this;
			},

			//=== PUBLIC ===
			sort : function(config) {
				var str = (this.isString(config))? '': 'sort=';
				this.setArray(config, this._sort, str);
				return this;
			},

			//=== PUBLIC ===
			limit : function(config){
				this._limit = '&limit='+config;
				return this;
			},

			//=== PUBLIC ===
			offset : function(config){
				this._offset = '&offset='+config;
				return this;
			},

			//=== PUBLIC === TODO $.ajaxのprocessDataを見て実装する事
			processDataFalse : function() {
//				this.encodeURI = function(e){return e};
				return this;
			},

			//=== PUBLIC ===
			parameter : function(p){
				this._data = p;
				return this;
			},

			//=== PUBLIC ===
			asJson : function() {
				console.log('======== this._data:'+this._data);
				var data = '';
				for(var i in this._filter)
					data+='&'+this._filter[i];
				for(var i in this._sort)
					data+='&'+this._sort[i];
				data+=this._limit;
				data+=this._offset;
				this._data=data.slice(1);
				this.log('GET', this._url + this._docType, this._data);
				this.clear();
				return this;
			},

			//=== PUBLIC ===
			get : function(data){
				var docType;
				if(data){
					this._data = (this.isString(data))? '/'+data:'/'+data._docId;//TODO $.ajaxではオブジェクトもリクエスト可能。その際にこのコードが必要か検証する。 
					this.log('GET', this._url + this._docType, this._data);
					this.clear();
				}else{
					docType=this._docType;
					this._docType='';
					this.log('GET', this._url + this._docType, this._data);
					this.clear();
					this._docType=docType;
				}
				return this;
			},

			//=== PUBLIC ===
			post : function(data) {
				_success = function(data, dataType){
					console.log('success()')
				}
				_error = function(data, dataType){
					console.log('error()')
				}
				var docType;
				if(data){
//					var dumper = new JKL.Dumper();
//					this._data = dumper.dump(data);
//					this.log('POST', this._url + this._docType, this._data);
					this._data = data;
					this.ajax('POST');
					this.clear();
				}else{
					this._data = '';
					this.ajax('POST');
					this.clear();
				}
				return null;
			},

			//=== PUBLIC ===
			put : function(data) {
				var docType;
				if(data){
					var dumper = new JKL.Dumper();
					this._data = dumper.dump(data);
					this.log('PUT', this._url + this._docType, this._data);
					this.clear();
				}else{
					docType=this._docType;
					this._docType='';
					this.log('PUT', this._url + this._docType, this._data);
					this.clear();
					this._docType=docType;
				}
				return this;
			},

			//=== PUBLIC ===
			delete : function(data) {
				var docType;
				if(data){
					this._data = (this.isString(data))? '/'+data:'/'+data._docId; //TODO $.ajaxではオブジェクトもリクエスト可能。その際にこのコードが必要か検証する。
					this.log('DELETE', this._url + this._docType, this._data);
					this.clear();
				}else{
//					docType=this._docType;
//					this._docType='';
//					this.log('DELETE', this._url + this._docType, this._data);
					this.ajax('DELETE');
					this.clear();
//					this._docType=docType;
				}
				return this;
			}

		}

	});
})(jQuery);


